/*
 * Copyright 2010 Assert Developments
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package be.ad.coffeebreak.web.commands;

import be.ad.coffeebreak.domain.User;
import be.ad.coffeebreak.exceptions.EmailAddressAlreadyUsedException;
import be.ad.coffeebreak.exceptions.ValidationException;
import be.ad.coffeebreak.services.SecurityService;
import be.ad.coffeebreak.web.Command;
import be.ad.coffeebreak.web.CommandContext;
import be.ad.coffeebreak.web.WebConstants;
import com.google.inject.Inject;

import java.util.HashMap;
import java.util.Map;

/**
 * An account command.
 *
 * @author Stefan Bangels
 */
public class AccountCommand implements Command {

    @Inject
    protected SecurityService securityService;

    /**
     * Set the security service.
     *
     * @param securityService the security service
     */
    @SuppressWarnings("unused")
    void setSecurityService(SecurityService securityService) {
        this.securityService = securityService;
    }

    /**
     * Execute the command using the specified command context.
     *
     * @param commandContext the command context
     * @return the view name
     * @see CommandContext
     */
    @Override
    public String execute(CommandContext commandContext) {
        User user = securityService.findUserById(1L);
        if (commandContext.isSubmit()) {
            saveUser(user, commandContext);
        }
        commandContext.setModel("user", user);
        commandContext.setModel("countries", WebConstants.COUNTRIES);
        commandContext.setModel("languages", WebConstants.LANGUAGES);
        commandContext.setModel("timezones", WebConstants.TIMEZONES);
        return WebConstants.VIEW_ACCOUNT;
    }

    /**
     * Save the user.
     *
     * @param user           the user
     * @param commandContext the command context
     */
    private void saveUser(User user, CommandContext commandContext) {
        fillUser(user, commandContext);
        Map<String, String> errors = new HashMap<String, String>();
        try {
            securityService.saveUser(user);
        } catch (ValidationException v) {
            errors.putAll(v.getErrors());
        } catch (EmailAddressAlreadyUsedException e) {
            errors.put("user.email", "account.error.user.email.duplicate");
        }
        commandContext.setModel("errors", errors);
    }

    /**
     * Fill the user by using the command context parameters.
     *
     * @param user           the user
     * @param commandContext the command context
     */
    private void fillUser(User user, CommandContext commandContext) {
        user.setEmail(commandContext.getParameter("email"));
        user.setName(commandContext.getParameter("name"));
        user.setDescription(commandContext.getParameter("description"));
        user.setLanguage(commandContext.getParameter("language"));
        user.setCountry(commandContext.getParameter("country"));
        user.setTimezone(commandContext.getParameter("timezone"));
    }

}
